/**
 * 
 */
package com.jdy.haoduoai.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.jdy.haoduoai.activity.PrivateStoryActivity;
import com.jdy.haoduoai.activity.RecordActivity2;
import com.jdy.haoduoai.model.CarlaClassicVo;
import com.jdy.haoduoai.model.DubItem;
import com.jdy.haoduoai.model.LeHotRecomStory;
import com.jdy.haoduoai.model.StoryCollectionItem;
import com.jdy.haoduoai.util.DateUtils;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.HorizontalIndicator;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import com.jdy.haoduoai.R;

/**
 * @author zp 2016-4-19 下午8:35:25
 */
public class DubStoryListAdapter extends BaseAdapter {

  private final Context mContext;

  private final LayoutInflater mInflater;

  private final ArrayList<StoryCollectionItem> mCatelogList;

  public DubStoryListAdapter(Context context,
      ArrayList<StoryCollectionItem> list) {
    this.mContext = context;
    this.mCatelogList = list;
    this.mInflater = LayoutInflater.from(mContext);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    if (this.mCatelogList != null)
      return this.mCatelogList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    if (this.mCatelogList != null)
      return this.mCatelogList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  private LeHotRecomStory convert2Lhr(DubItem di) {
    // TODO Auto-generated method stub
    LeHotRecomStory lhr = new LeHotRecomStory();
    lhr.tags = di.tags;
    lhr.enabled = di.enabled;
    lhr.sort = di.sort;
    lhr.screenshot = di.screenshot;
    lhr.play_count = di.play_count;
    lhr.catalog = di.catalog;
    lhr.age_interval = di.age_interval;
    lhr.deleted_at = di.deleted_at;
    lhr.category_id = di.category_id;
    lhr.url = di.url;
    lhr.corner_icon = di.corner_icon;
    lhr.id = di.id;
    lhr.author = di.author;
    lhr.followers = di.followers;
    lhr.cover = di.cover;
    lhr.updated_at = di.updated_at;
    lhr.name = di.name;
    lhr.length = di.length;
    lhr.created_at = di.created_at;
    lhr.lines = di.lines;

    return lhr;
  }

  private CarlaClassicVo convert2CarlaClassicVo(LeHotRecomStory recommendStroy) {
    // TODO Auto-generated method stub
    if (recommendStroy == null)
      return null;
    LeHotRecomStory lhr = recommendStroy;
    CarlaClassicVo ccv = new CarlaClassicVo();
    ccv.id = lhr.id; // );
    ccv.category_id = lhr.category_id; // );
    ccv.catalog = lhr.catalog; // );
    ccv.name = lhr.name; // );
    ccv.cover = lhr.cover; // );
    ccv.url = lhr.url; // );
    ccv.enabled = lhr.enabled; // );
    ccv.sort = lhr.sort; // );
    ccv.length = lhr.length; // );
    ccv.agefrom = 0;
    ccv.ageto = 0;
    ccv.lines = lhr.lines;// );
    ccv.created_at = lhr.created_at; // );
    ccv.updated_at = lhr.updated_at; // );
    ccv.deleted_at = lhr.deleted_at; // );
    ccv.followers = lhr.followers; // );
    ccv.corner_icon = lhr.corner_icon; // );
    ccv.author = lhr.author; // );
    ccv.tags = lhr.tags;// );
    return ccv;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final StoryCollectionItem sci = mCatelogList.get(position);
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.cartoonlist_item, null);
      holder.mIconImg = (ImageView) convertView.findViewById(R.id.icon_img);
      holder.mTitleTv = (TextView) convertView
          .findViewById(R.id.catelog_title_tv);
      holder.mMoreImg = (ImageView) convertView.findViewById(R.id.more_img);
      holder.mHRootLayout = (LinearLayout) convertView
          .findViewById(R.id.h_layout);

      final HorizontalScrollView hsv = (HorizontalScrollView) convertView
          .findViewById(R.id.h_scrollview);

      holder.mHScrollView = hsv;

      final HorizontalIndicator hi = (HorizontalIndicator) convertView
          .findViewById(R.id.h_indicator);

      holder.mHIndicator = hi;

      hsv.getViewTreeObserver().addOnScrollChangedListener(
          new OnScrollChangedListener() {

            @Override
            public void onScrollChanged() {

              int deltaX = hsv.getScrollX();

              DisplayMetrics metrics = mContext.getResources()
                  .getDisplayMetrics();

              int maxX = hsv.getChildAt(0).getMeasuredWidth()
                  - metrics.widthPixels + 5;

              hi.onScrollChanged(deltaX, maxX);
            }
          });

      if (Tools.isNotEmpty(sci.getStory_collection())) {
        int i = 0;
        ArrayList<DubItem> aciList = sci.getStory_collection();
        int size = aciList.size();
        for (; i < size; i++) {
          final DubItem di = aciList.get(i);
          View hChild = mInflater.inflate(R.layout.horizontallist_item, null);
          ImageView coverImg = (ImageView) hChild.findViewById(R.id.img);
          coverImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
              // TODO Auto-generated method stub

              LeHotRecomStory lhr = convert2Lhr(di);
              Intent recode = new Intent();
              Bundle bundle = new Bundle();
              bundle.putSerializable("carlaclassic",
                  convert2CarlaClassicVo(lhr));
              recode.putExtras(bundle);
              recode.setClass(mContext, RecordActivity2.class);
              mContext.startActivity(recode);

            }

          });
          TextView titleTv = (TextView) hChild.findViewById(R.id.tittle);
          titleTv.setText(di.name);
          TextView timeLengthTv = (TextView) hChild
              .findViewById(R.id.time_length_tv);
          // timeLengthTv.setVisibility(View.VISIBLE);
          timeLengthTv.setText(DateUtils.getTime(di.length));

          if (Tools.isNotEmpty(di.cover))
            Picasso
                .with(mContext)
                .load(di.cover)
                .fit()
                .centerCrop()
                .placeholder(
                    mContext.getResources().getDrawable(
                        R.drawable.default_cover))
                .error(
                    mContext.getResources().getDrawable(
                        R.drawable.default_cover)).into(coverImg);
          LayoutParams params_image = new LayoutParams(
              LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
          params_image.setMargins(Tools.getPixelByDip(mContext, 5), 0,
              Tools.getPixelByDip(mContext, 5), 0);
          hChild.setLayoutParams(params_image);
          holder.mHRootLayout.addView(hChild);
        }
      }
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.mIconImg.setVisibility(View.GONE);
    holder.mTitleTv.setText(sci.getName());
    holder.mMoreImg.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent();
        intent.setClass(mContext, PrivateStoryActivity.class);
        intent.putExtra("titles", sci.getName());
        intent.putExtra("category_id", sci.getId());
        mContext.startActivity(intent);
      }
    });

    return convertView;
  }

  private class ViewHolder {

    public ImageView mIconImg;

    public TextView mTitleTv;

    public ImageView mMoreImg;

    public LinearLayout mHRootLayout;

    public HorizontalScrollView mHScrollView;

    public HorizontalIndicator mHIndicator;
  }

}
